package Anagram;

import java.io.*;
import java.util.*;

/**
 * User: Vitalii Serdiuk
 * Date: 30.11.13 16:35
 */
public class TextAnalyser {
    public Map<String, Set<Word>> buildWordCode(Collection<Word> words) {
        Map<String, Set<Word>> wordsTable = new HashMap<String, Set<Word>>();

        for (Word word : words) {
            if (word.length() < 2) continue;

            String key = generateKey(word);

            Set<Word> possibleAnagram = wordsTable.get(key);
            if (possibleAnagram == null) {
                possibleAnagram = new HashSet<Word>();
            }
            possibleAnagram.add(word);

            wordsTable.put(key, possibleAnagram);
        }
        return wordsTable;
    }

    public String generateKey(Word word) {
        char[] codeKey = word.toString().toCharArray();
        Arrays.sort(codeKey);
        return new String(codeKey);
    }
}